package com.ordersystem.controller;

import com.ordersystem.domain.DishCategory;
import com.ordersystem.domain.Result;
import com.ordersystem.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : CategoryController
 * @description : [描述说明该类的功能]
 * @createTime : [2022/3/26 12:53]
 * @updateUser : asus1020
 * @updateTime : [2022/3/26 12:53]
 * @updateRemark : [描述说明本次修改内容]
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/findCategorys")//查询所有分类信息
    public Result findCategorys(){
        Result result = new Result();
        List<DishCategory> categorys = categoryService.findCategorys();
        if (categorys!=null){
            result.setStatus(200);
            result.setMessage("查询菜品分类信息成功");
            result.setData(categorys);
        }else {
            result.setStatus(500);
            result.setMessage("查询菜品分类信息失败");
        }
        return result;
    }
    @GetMapping("/findCategoryOne/{cid}")//查询单个菜品
    public Result findCategoryById(@PathVariable("cid") Integer cid){
        Result result = new Result();
        DishCategory dishCategory = categoryService.findCategoryById(cid);
        if (dishCategory!=null){
            result.setStatus(200);
            result.setMessage("查询菜品分类信息成功");
            result.setData(dishCategory);
        }else {
            result.setStatus(500);
            result.setMessage("查询菜品分类信息失败");
        }
        return result;
    }
    @DeleteMapping("/deleteCategory/{cid}")//删除单个菜品分类信息
    public Result deleteCategory(@PathVariable("cid") Integer cid){
        Result result = new Result();
        int dishNumber = categoryService.statisticsDishNumberByCid(cid);
        if (dishNumber!=0){
           return Result.fail("当前分类下已绑定菜品,不能删除！");
        }
        int total = categoryService.deleteCategory(cid);
        if (total>0){
            result.setStatus(200);
            result.setMessage("删除菜品分类信息成功");
        }else {
            result.setStatus(500);
            result.setMessage("删除菜品分类信息失败");
        }


        return result;
    }
    @PutMapping("/updateCategory")//修改菜品分类信息
    public Result updateCategory(@RequestBody DishCategory dishCategory){
        Result result = new Result();
        int total= categoryService.updateCategory(dishCategory);
        if (total>0){
            result.setStatus(200);
            result.setMessage("修改菜品分类信息成功");
            result.setData(categoryService.findCategoryById(dishCategory.getCid()));
        }else {
            result.setStatus(500);
            result.setMessage("修改菜品分类信息失败");
        }
        return result;
    }
    @PostMapping("/addCategory")//添加菜品分类信息
    public Result addCategory(@RequestBody DishCategory dishCategory){
        Result result = new Result();
        int total= categoryService.addCategory(dishCategory);
        if (total>0){
            result.setStatus(200);
            result.setMessage("添加菜品分类信息成功");
        }else {
            result.setStatus(500);
            result.setMessage("添加菜品分类信息失败");
        }
        return result;
    }

}
